#include "GameHighScore.h"
#include "ui/CocosGUI.h"
#include "Constants.h"
#include "MainScene.h"

using namespace cocos2d;
using namespace ui;

bool GameHighScore::init() {
	if (!Layer::init()) {
		return false;
	}

	auto visibleSize = Director::getInstance()->getVisibleSize();

	auto bg = ImageView::create("UI/ResultBK.png");
	bg->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2));
	this->addChild(bg);

	auto titleLabel = ImageView::create("UI/HighScore_Label.png");
	titleLabel->setNormalizedPosition(Vec2(0.5f, 0.85f));
	bg->addChild(titleLabel);

	auto username = TextField::create("", "", 30);
	username->setNormalizedPosition(Vec2(0.5f, 0.75f));
	bg->addChild(username);

	auto scoreLabel = ImageView::create("UI/score_words.png");
	scoreLabel->setNormalizedPosition(Vec2(0.45f, 0.65f));
	bg->addChild(scoreLabel);

	auto scoreNum = LabelAtlas::create("0", "UI/number.png", 22, 34, '0');
	scoreNum->setAnchorPoint(Vec2(0.0f, 0.5f));
	scoreNum->setNormalizedPosition(Vec2(0.55f, 0.65f));
	bg->addChild(scoreNum);

	auto start = Button::create("UI/start.png", "UI/start_down.png");
	start->setNormalizedPosition(Vec2(0.5f, 0.2f));
	bg->addChild(start);

	username->setString(UserDefault::getInstance()->getStringForKey("High_Name", "$_$"));

	int _score = UserDefault::getInstance()->getIntegerForKey("High_Score", 0);
	char c_score[32] = { 0 };
	sprintf(c_score, "%d", _score);
	scoreNum->setString(c_score);

	start->addTouchEventListener([&](Ref* sender, Widget::TouchEventType type) {
		dynamic_cast<MainScene*>(this->getParent())->gameControl(PlayGame);
	});

	return true;
}